package com.hdc.hdc.mainmenu2.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdc.hdc.mainmenu2.entity.CustomerCompany;
import com.hdc.hdc.mainmenu2.entity.vo.MailMessageVO;
import com.hdc.hdc.mainmenu2.mail.MailHelp;
import com.hdc.hdc.mainmenu2.service.CompanyServiceI;
import com.hdc.hdc.mainmenu2.util.IsNotNullValit;
import com.hdc.hdc.mainmenu2.util.ReturnJson;

/**
 * 通讯页面
 * @author gxy
 *
 * @date 2018年4月6日 下午7:26:20
 *
 *
 * sucess dependency on not give up
 *
 */

@Controller
public class ContactController {
	
	
	private static final Logger log = LoggerFactory.getLogger(ContactController.class);
	
	@Autowired
	private CompanyServiceI companyServiceI;
	
	@Autowired
	private MailHelp mailSender;

	
	@RequestMapping("/contact/{companyCode}")
	public String gotoContact(@PathVariable String companyCode,Model model) {
		
		log.info("当前请求的公司代码："+companyCode);
		
		if (!IsNotNullValit.StringIsNotNull(companyCode)) {
			return "exception/ex";
		}
		
		CustomerCompany company = this.companyServiceI.getCompanyMessageByCode(companyCode);
		
		model.addAttribute("company",company);
		
		return "/contact/contact";
	}
	
	@PostMapping(value="/sendMail")
	@ResponseBody
	public ReturnJson sendMail(@RequestBody MailMessageVO vo) {
		
		ReturnJson j = new ReturnJson();
		
		try {
			
			vo.setCreateDate(new Date());
			
			j = mailSender.sendEmailToCustomer(vo);
			
		} catch (Exception e) {
			e.printStackTrace();
			j = j.fail("提交失败，请手动发送邮箱或者打电话到上面显示的联系方式。非常抱歉");
		}
		
		return j;
	}
	

}
